<!DOCTYPE html>
<html lang="en">
	<head>
		<meta charset="UTF-8">
		<title>新增型号</title>
		<link rel="stylesheet" type="text/css" href="/webapp/css/easyui.css">
	    <link rel="stylesheet" type="text/css" href="/webapp/css/icon.css">
        <link rel="stylesheet" type="text/css" href="/webapp/css/color.css">
        <link rel="stylesheet" type="text/css" href="/webapp/css/public.css">
        <link rel="stylesheet" type="text/css" href="/webapp/css/model.css">
	    <script type="text/javascript" src="/webapp/js/lib/jquery.min.js"></script>
	    <script type="text/javascript" src="/webapp/js/lib/jquery.easyui.min.js"></script>
        <script type="text/javascript" src="/webapp/js/lib/jquery.global.settings.js"></script>
	    <script type="text/javascript" src="/webapp/js/lib/ajaxfileupload.js"></script>
	    <script type="text/javascript" src="/webapp/js/common/config.js"></script>
        <script type="text/javascript" src="/webapp/js/common/metadata.js"></script>
        <style type="text/css">
            .lv {
                background-color:#16ab8c;
                color:#FFF;
            }
            .red {
                background-color: #E20A0A;
                color: #ffffff;
            }
            .orange{
                background-color: #e47833;
                color: #ffffff;
            }
            .yellow {
                background-color: #FAFAD2;
                color: #000000;
            }
        </style>
	</head>

	<body>
    <div class="box4">
        <!--<div style="width: 100px; height: 100px"></div>-->
        <div class="imgAnalyzeDiv" id="imgAnalyzeDiv">
            <label class="item-label">识别信息：</label>
            <select class="easyui-combobox" id="imgAnalyzeSelect" style="width:150px;" ></select>
            <a class="easyui-linkbutton c8" href="javascript:void(0);" onclick="imgModelConfirm()" style="width:45px;margin-left: 3px;display: none" id="imgModelConfirm">确认</a>
            <a class="easyui-linkbutton" href="javascript:void(0);" onclick="cleanAnalyze()" style="width:45px;margin-left: 3px;">clean</a>
            <a class="easyui-linkbutton c2" href="javascript:void(0);" onclick="againAnalyze()" style="width:100px;margin-left: 3px;">重新识别</a>
            <input class="easyui-searchbox" data-options="prompt:'Please Input',searcher:imgSearch" style="width:150px" id="inputSearch"/>
        </div>
        <div class="jdImg" id="jdImg" style="height: 685px;top: 130px;">
            <!--<div class="img_content" id="img_content"></div>-->
        </div>
        <div class="propDetail" id="propDetail" style="top: 50px;" >
            <table id="propTable" cellspacing="0">
            </table>
        </div>
    </div>
              <div id="nav" class="nav"></div>
              <div class="easyui-panel" style="border:none;">
              	<form id="ff" name="model">
              		<div style="margin-bottom:20px;">
                        <label class="item-label"><span class="item-required">*</span>品类：</label>
                       <select class="easyui-combobox" id="categorySelect" name="categoryCode" style="width:155px;" required="required" >
                       </select>
                       <label class="item-label"><span class="item-required">*</span>品牌：</label>
                       <select class="easyui-combobox" id="brandSelect" name="brandCode" style="width:155px;" validType="notNull['#brandSelect']" required="required" data-options="icons: [{ iconCls: 'icon-reload', handler: function (e) { aa() }}]">
                        </select>
                        <a class="easyui-linkbutton c8" href="javascript:void(0);" onclick="searchBymodelf()" style="width:100px;margin-left: 93px;">型号确认</a>&nbsp;&nbsp;
                  	</div>
                    <div style="margin-bottom:20px;">
                        <label class="item-label"><span class="item-required">*</span>系列：</label>
                        <input type="text" class="easyui-textbox " name="seriesName" id="seriesName"   style="width:155px;"data-options="required:true"/>
                        <label class="item-label"><span class="item-required">*</span>型号：</label>
                        <input type="text" class="easyui-textbox " name="modelName" id="modelName"  style="width:155px;" data-options="required:true"/>
                        <label class="item-label"><span class="item-required">*</span>sku名称：</label>
                        <input type="text" class="easyui-textbox " name="skuName" id="skuName"  style="width:155px;" data-options="required:true"/>
                    </div>
                    <div style="margin-bottom:20px;display:inline-block;">
                        <div style="display:inline">
                            <label class="item-label"><span class="item-required">*</span>url：</label> <!--data-options="multiline:true"-->
                            <input type="text" class="easyui-textbox" name="url"  id="url" style="width:548px;min-height:66px;"data-options="required:true"/>
                        </div>
                        <div style="display:inline-block;width: 110px;word-break:break-all;margin-top: -10px;">
                            <button style="width:50px;" onclick="showImage()" >图片</button>
                            <button style="width:50px;" onclick="imageAnalyze()" >识别</button>
                            <button style="width:50px;margin-top: 3px;" onclick="tzwy()" >跳转</button>
                            <button style="width:50px;margin-top: 3px;" onclick="copyUrl()" >复制</button>
                        </div>
                    </div>
                    <div style="margin-bottom:20px;">
                        <label class="item-label"></span>itemId：</label>
                        <input type="text" class="easyui-textbox" name="itemId" id="itemId"  style="width:155px;"readonly="readonly"/>
                        <label class="item-label">skuId：</label>
                        <input type="text" class="easyui-textbox " name="skuId" id="skuId"  style="width:155px;" readonly="readonly"/>
                        <label class="item-label" ><span class="item-required">*</span>规格状态：</label>
                        <select name="propStatus" class="easyui-combobox"  style="width:155px;" data-options="required:true,panelHeight:'auto'"   >
                            <option value="CONFIRMONE" selected>部分确认</option>
                            <option value="CONFIRMALL">全部确认</option>
                            <option value="UNCOFIRMED">未确认</option>
                            <option value="UNABLECOFIRED">无法找到信息</option>
                            <option value="UNRELEASED">暂未发布</option>
                        </select>
                    </div>
                    <div style="margin-bottom:20px;">
                        <label class="item-label"><span class="item-required">*</span>标题型号：</label>
                        <input type="text" class="easyui-textbox " name="titleModel" id="titleModel"  style="width:155px;" data-options="required:true"/>
                        <label class="item-label"><span class="item-required">*</span>详情型号：</label>
                        <input type="text" class="easyui-textbox " name="detailsModel" id="detailsModel" style="width:155px;" data-options="required:true"/>
                        <label class="item-label"><span class="item-required">*</span>图片型号：</label>
                        <input type="text" class="easyui-textbox " name="pictureModel" id="pictureModel"  style="width:155px;" data-options="required:true"/>
                    </div>
                    <div style="margin-bottom:20px;">
                        <label class="item-label"><span class="item-required">*</span>页面划线价：</label>
                        <input type="text" class="easyui-numberbox " name="underlinedPrice" id="underlinedPrice"  style="width:155px;" data-options="required:true"/>
                        <label class="item-label"><span class="item-required">*</span>页面标价：</label>
                        <input type="text" class="easyui-numberbox " name="onSellPrice" id="onSellPrice" style="width:155px;" data-options="required:true"/>
                        <label class="item-label"><span class="item-required">*</span>页面优惠价：</label>
                        <input type="text" class="easyui-numberbox " name="discountedPrice" id="discountedPrice"  style="width:155px;" data-options="required:true"/>
                    </div>
                    <div style="margin-bottom:20px;">
                        <label class="item-label" >上市年：</label>
                        <input type="text" class="easyui-numberbox" name="onSellYear" id="onSellYear"  style="width:155px;"/>
                        <label class="item-label" >上市月：</label>
                        <input type="text" class="easyui-numberbox" name="onSellMonth" id="onSellMonth"  style="width:155px;"/>
                        <label class="item-label" >上市周：</label>
                        <input type="text" class="easyui-numberbox" name="onSellWeek" id="onSellWeek" style="width:155px;"/>
                    </div>
                    <div style="margin-bottom:20px;">
                        <label class="item-label">别名：</label>
                        <input name="otherNames" class="easyui-tagbox" value="" style="width:280px;min-height:60px;" data-options="multiline:true"/>

                        <label class="item-label" style="margin:10px 0;"></span>卖点：</label>
                        <input name="sellMerit"  class="easyui-tagbox" value="" style="width:280px;min-height:60px;" data-options="multiline:true"/>
                    </div>

                    <div >
                        &nbsp;&nbsp;<span style="font-weight: 900 ;font-size:20px; width: 100px" >规格信息：</span>

                    </div>
                    <div id="categorymetadatawrapper" style="width: 740px"></div>
              		<div style="margin-bottom:20px;margin-left:100px;">
                         <a href="javascript:void(0)" class="easyui-linkbutton c8" onclick="submitForm()" style="width:80px">保存</a>
                         <a href="javascript:void(0)" class="easyui-linkbutton" onclick="cancel()" style="width:80px">取消</a>
                  </div>
              	</form>
              </div>
            </div>
        </div>
	</body>
  <script type="text/javascript">
      var id=getQueryParameter("id"),categoryCode=getQueryParameter("categoryCode");
      var categoryType;
      var seriesTo3cCode;
      $(function(){
        setNav();
        getCategoryType();
        if(id){
            $.when(initBrandSelectC("brandSelect", function () {initSeries(categoryCode,"brandSelect")},null,categoryCode),
                initCategorySelect("categorySelect"),
                generatemetadatawrap(false)
            ).done(function(){initFormData()});
        }else{
            $.when(initBrandSelectC("brandSelect", function () {initSeries(categoryCode,"brandSelect")},null,categoryCode),
                initCategorySelect("categorySelect"),
                generatemetadatawrap(true)
            ).done(function(){initFormData()});
        }
          initOnSellYearMonth();
          //自动获取itemID
          $("input", $("#url").next("span")).blur(function () {
              $('#skuId').textbox("setValue", "");
              $('#itemId').textbox("setValue", "");
              var name = $('#url').textbox("getValue");
              if (name) {
                  var itemId="";
                  //赋值
                  if(name.indexOf("https://item.jd.com/")!=-1){
                      itemId=name.replace(/[^0-9]/ig,"");
                      // alert("京东网址,itemId:"+itemId)
                  }else if(name.indexOf("https://detail.tmall.com")!=-1){
                      var Request=new tmUrlSearch(name); //实例化
                      itemId=Request.id;
                      var skuId=Request.skuId;
                      if(itemId){
                          itemId=itemId.replace(/[^0-9]/ig,"")
                      }
                      if(skuId){
                          skuId=skuId.replace(/[^0-9]/ig,"")
                      }
                      // alert(Request.id);
                      // var itemId=GetQueryString(name,"id");
                      // alert("天猫网址,itemId:"+itemId+",skuId:"+skuId)
                      $('#skuId').textbox("setValue", skuId);
                  }
                  $('#itemId').textbox("setValue", itemId);
              }
          });
      });
      //天猫url截取
      function tmUrlSearch(str) {
          var name,value;
          var num=str.indexOf("?")
          str=str.substr(num+1); //取得所有参数   stringvar.substr(start [, length ]

          var arr=str.split("&"); //各个参数放到数组里
          for(var i=0;i < arr.length;i++){
              num=arr[i].indexOf("=");
              if(num>0){
                  name=arr[i].substring(0,num);
                  value=arr[i].substr(num+1);
                  this[name]=value;
              }
          }
      }
      //天猫url截取
      function GetQueryString(url,name) {
          var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
          var r = url.substr(1).match(reg);
          if(r!=null)
              return  unescape(r[2]);
          return null;
      }
      function getCategoryType() {
          $.ajax({
              url: config.url + "/category/get",
              type: "get",
              data: {code:categoryCode},
              xhrFields: {withCredentials: true},
              success: function (data) {
                  if (data.code == 200) {
                      categoryType = data.msg.type;
                    /*  if (categoryType == 2) {
                          $("#seriesTo3cCode").next().show();
                          $("#series").next().hide();
                          $("#nameDiv").hide();
                          $("#modelName").textbox("setValue", "1"); //3c产品不用写型号，初始化name用于表单验证
                      }else {
                          $("#seriesTo3cCode").next().hide();
                      }*/
                  }
              },
              error: function (XMLHttpRequest, textStatus, errorThrown) {

              }
          });
      }

      function generatemetadatawrap(au) {//au为布尔类型，当为true时表示添加数据
          $.ajax({
              url:config.url+"/categoryMetadata/getMetadataUnderCategory?categoryCode="+categoryCode,
              type:'get',
              xhrFields:{withCredentials:true},
              success:function (data) {
                  //规格数据填充
                  var content = generateContent(data,au);
                  $("#categorymetadatawrapper").html(content);
                  $.parser.parse($("#categorymetadatawrapper"));

                  /**
                   * 针对智能门锁进行关联项
                   */

                  $(".zw").change(function(a) {
                      if($(".zw").prop('checked')){
                          $("#04774").combobox("setValue","是");
                          // alert("选中");
                    //当前为选中状态
                      }else {
                          $("#04774").combobox("setValue","否");
                          // alert("未选中");
                    //当前为不选中状态
                      }
                  });
                  $(".mm").change(function(a) {
                      if($(".mm").prop('checked')){
                          $("#04775").combobox("setValue","是");
                    //当前为选中状态
                      }else {
                          $("#04775").combobox("setValue","否");
                    //当前为不选中状态
                      }
                  });

                  $(".ys").change(function(a) {
                      if($(".ys").prop('checked')){
                          $("#04776").combobox("setValue","是");
                    //当前为选中状态
                      }else {
                          $("#04776").combobox("setValue","否");
                    //当前为不选中状态
                      }
                  });
                  $(".sk").change(function(a) {
                      if($(".sk").prop('checked')){
                          $("#04777").combobox("setValue","是");
                    //当前为选中状态
                      }else {
                          $("#04777").combobox("setValue","否");
                    //当前为不选中状态
                      }
                  });
                  $(".app").change(function(a) {
                      if($(".app").prop('checked')){
                          $("#04778").combobox("setValue","是");
                    //当前为选中状态
                      }else {
                          $("#04778").combobox("setValue","否");
                    //当前为不选中状态
                      }
                  });
                  $(".rlsb").change(function(a) {
                      if($(".rlsb").prop('checked')){
                          $("#04779").combobox("setValue","是");
                    //当前为选中状态
                      }else {
                          $("#04779").combobox("setValue","否");
                    //当前为不选中状态
                      }
                  });
                  $(".wx").change(function(a) {
                      if($(".wx").prop('checked')){
                          $("#04780").combobox("setValue","是");
                    //当前为选中状态
                      }else {
                          $("#04780").combobox("setValue","否");
                    //当前为不选中状态
                      }
                  });
                  $(".hm").change(function(a) {
                      if($(".hm").prop('checked')){
                          $("#04781").combobox("setValue","是");
                    //当前为选中状态
                      }else {
                          $("#04781").combobox("setValue","否");
                    //当前为不选中状态
                      }
                  });
                  $(".zjm").change(function(a) {
                      if($(".zjm").prop('checked')){
                          $("#04782").combobox("setValue","是");
                    //当前为选中状态
                      }else {
                          $("#04782").combobox("setValue","否");
                    //当前为不选中状态
                      }
                  });
                  $(".ly").change(function(a) {
                      if($(".ly").prop('checked')){
                          $("#04783").combobox("setValue","是");
                    //当前为选中状态
                      }else {
                          $("#04783").combobox("setValue","否");
                    //当前为不选中状态
                      }
                  });
                  //针对
                  $("#05213").combobox({
                      onSelect: function (a) {
                          if(a.value =="足浴盆"){
                              var fruits = ["腿部", "脚部"]
                              $("#05220").combobox("setValues",fruits);
                          }else{
                              $("#05220").combobox("setValues","");
                          }
                      }

                  });

              }
          });
      }
      function initFormData(){
	        if(id){
	            $.ajax({
                    url:config.url +"/seriesModel/"+id,
                    type:'get',
	              xhrFields:{withCredentials:true},
	              success:function(data){
	                //填充表单
	                  if(data){
	                      var formdata = changeProperty2String(data);
	                      if(!formdata.otherNames){
	                          formdata.otherNames=[];
                          }
                          if(!formdata.sellMerit){
	                          formdata.sellMerit=[];
                          }
                          if(!formdata.description){
                              formdata.description=[];
                          }
                          if(formdata.properties){
	                          var props = formdata.properties;
	                          for(var i in props){
                                  if(props[i].value){
                                      $("#c"+props[i].code).attr("class","item-label");
                                  }
                                  if(props[i].value&& -1==props[i].value){
                                      formdata[props[i].code] = "-1";
                                  }else{
                                      formdata[props[i].code] = props[i].value;
                                  }
                                  if("boolean"==(typeof props[i].value)){
                                      formdata[props[i].code] = props[i].value.toString();
                                  }
                              }
                          }
                          seriesTo3cCode = formdata.seriesTo3cCode;
                          $.when($("#ff").form("load",formdata)).done(function(){
                              $('#seriesTo3cCode').combobox("setValue", seriesTo3cCode)
                          });
                          //model brand readonly
                          $("#brandSelect").combobox("readonly",true);
	                  }
	              },
	              error:function(){

	              }
	            });
            }
            $("#categorySelect").combobox("select",categoryCode);
	        $("#categorySelect").combobox("readonly",true);
      }

      function submitForm(){
          $("#ff").form("submit",{
               onSubmit:function(){
                  var valid = $(this).form('enableValidation').form('validate');
                  if(valid){
                      var formData = getFormData(document.getElementById("ff"));
                      formData = transfer(formData);
                      var method = "POST";
                      if(id){
                          method = "PATCH";
                          formData.id = id;
                      }
                      if(formData['otherNames'] && typeof formData['otherNames'] === 'string'){
                          formData['otherNames'] = [formData['otherNames']]
                      }
                      if(formData['sellMerit'] && typeof formData['sellMerit'] === 'string'){
                          formData['sellMerit'] = [formData['sellMerit']]
                      }
                      formData.url = $("#url").textbox('getValue');
                      var properties = [];
                      for(var i in formData){
                          if(!isNaN(i)&&formData[i]){
                              properties.push({code:i,value:formData[i]});
                          }
                      }
                      formData['properties'] = properties;
                      //验证系列
                      var series = formData["seriesName"];
                      var reg = /^(\w|[\u4E00-\u9FA5]|\(|\)|\/|-)*$/;
                      if (series) {
                          if (series.length > 20) {
                              $.messager.alert("提示","【系列】长度不能超过20个字符","error");
                              return;
                          }
                          // else if(!series.match(reg)){
                          //     $.messager.alert("提示","【系列】不能含有\"()/-\"以外的特殊符号","error");
                          //     return;
                          // }
                      }

                      if(formData['onSellPrice']<=0){
                          $.messager.alert("提示","上市价格不能小于等于0","error");
                          return;
                      }

                      if (!id) {
                          if (!formData['onSellYear']) {
                              formData['onSellYear'] = new Date().getFullYear();
                          }
                          if (!formData['onSellMonth']) {
                              formData['onSellMonth'] = new Date().getMonth()+1;
                          }
                          if (!formData['onSellWeek']) {
                              formData['onSellWeek'] = window.parent.timeData.weekInYear;;
                          }
                      }
                      if(formData.url.indexOf("https://detail.tmall.com")!=-1 || formData.url.indexOf("https://item.jd.com/")!=-1){
                          if(!formData.itemId){
                              $.messager.alert("提示","天猫/京东网址，itemId不允许为空","error");
                              return;
                          }
                          //后期skuId也不能为空
                      }
                      $.ajax({
                         url:config.url+"/seriesModel",
                         type:method,
                         data:JSON.stringify(formData),
                         dataType:'json',
                         contentType:'application/json;charset=UTF-8',
                         xhrFields:{withCredentials:true},
                         beforeSend:function(){
                             $.messager.progress({
                                 title:"请稍等",
                                 interval:50,
                                 text:"操作进行中..."
                             });
                         },
                         complete:function(){
                              $.messager.progress("close");
                         },
                          success:function(data){
                             // console.log(data.code);
                              if(data.code=="200"){
                              $.messager.alert("提示",data.msg,"info",function(){
                                 window.parent.closeCurrentTab(true);
                              });
                              }else{
                                  $.messager.alert("提示",data.msg,"error");
                              }
                          },
                          error:function(XMLHttpRequest,textStatus,thrown){
                              $.messager.alert("提示","操作失败","error");
                          }
                      });
                       return false;
                    }
                    return false;
               }
          });
      }
      function aa(){
          $.ajax({
              url:config.url + "/cron/cache/data/refresh",
              type:'get',
              xhrFields:{withCredentials:true},
              beforeSend:function () {
                  $.messager.progress({
                      title:"请稍等",
                      interval:50,
                      text:"数据加载中..."
                  });
              },
              complete:function () {
                  $.messager.progress("close");
              },
              success:function (data) {

              },
              error:function (XMLHttpRequest,textStatus,errorThrown) {

              }
          });
          initBrandSelect("brandSelect");
      }
      function cancel(){
    	  window.parent.closeCurrentTab();
      }

      function searchBymodelf() {
          debugger
          var params = {};
          params.id=id;
          var formdata = getFormData(document.getElementById("ff"));
            //itemId存在根据itemId查重，不存在根据型号+品牌+品类查
          if(! formdata.itemId){
              params.categoryCode = formdata.categoryCode;
              params.brandCode = formdata.brandCode;
              params.name = formdata.modelName;
          }else{
              params.categoryCode = formdata.skuId;
              params.brandCode = formdata.itemId;//借用品牌查型号是都重复
          }
          params.model = false;
          $.ajax({
              url: config.url + "/workshop/modelCheck",
              type: "post",
              data: params,
              xhrFields: {withCredentials: true},
              success: function (data) {
                  if (data == "") {
                      $.messager.alert("提示", "该型号不存在，可以添加", "info")
                  } else {
                      $.messager.alert("提示", data, "error");
                  }
              },
              error: function (XMLHttpRequest, textStatus, errorThrown) {

              }
          });
      }

      function copyUrl(){
          var  url=$("#url").textbox('getValue');
          console.log(url);
          var flag = copyText(url);
          $.messager.show({
              title: '提示',
              msg: flag?"网址复制成功":"网址复制失败",
              timeout: 500,
              showType: 'show'
          });
      }
      function tzwy() {
          var  wz=$("#url").textbox('getValue');
          window.open(wz);
      }
      function initOnSellYearMonth() {
          if(!id){
              var myDate = new Date();
              var year = myDate.getFullYear();//获取年
              var month = myDate.getMonth() + 1;//获取月，默认从0开始，所以要加一
              $('#onSellYear').numberbox({prompt:year});
              $('#onSellMonth').numberbox({prompt:month});
             var week = window.parent.timeData.weekInYear;
             $('#onSellWeek').numberbox({prompt:week});
          }
      }

        // 反转义

      function HTMLDecode(text) {
          var temp = document.createElement("div");
          temp.innerHTML = text;
          var output = temp.innerText || temp.textContent;
          temp = null;
          return output;
      }

      //HTML转义
      function HTMLEncode(html) {
          var temp = document.createElement("div");
          (temp.textContent != null) ? (temp.textContent = html) : (temp.innerText = html);
          var output = temp.innerHTML;
          temp = null;
          return output;
      }




  </script>
</html>